package com.anolesoft.yr.mbus.contm.dto;

import com.anolesoft.yr.mbus.contm.entity.ContmContent;
import com.anolesoft.yr.store.stk.util.StringUtil;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;



@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ContmContentDto {
/*
    */
/**
     * 根据json形成一个ContmContentDto
     * @param contmDtoJson
     *//*

    public ContmContentDto(JSONObject contmDtoJson, ContmContentDto parent,int orderCode,int lvl,List<ContmStyleItem> contmStyleItems){
        this.id  = contmDtoJson.getString("id");
        this.text = contmDtoJson.getString("text");
        this.html = contmDtoJson.getJSONObject("attributes").getString("html");
        this.contMmainUid = contmDtoJson.getJSONObject("attributes").getString("contmMainUid");
        this.orderCode = orderCode;
        this.parent = parent;
        this.pageType = contmDtoJson.getJSONObject("attributes").getString("contmMainUid")==null?contmDtoJson.getJSONObject("attributes").getString("contmMainUid"): getParent().getPageType();
        JSONArray children = contmDtoJson.getJSONArray("children");
        List<ContmContentDto> contmContentDtos = new ArrayList<>();
        for (int i = 0; i < children.size(); i++) {
            contmContentDtos.add(new ContmContentDto(children.getJSONObject(i),this,i,lvl+1,contmStyleItems));
        }
        this.lvl = lvl;
        if(pageType.equals("sy")){
            type = "sy_bt";
            contmStyleItems.forEach(a->{
                if(a.getItemType().equals("sy_bt")){
                    this.styleItemUid = a.getObjuid();
                }
            });

        }else if(pageType.equals("fy")){
            type = "fy_bt";
            contmStyleItems.forEach(a->{
                if(a.getItemType().equals("fy_bt")){
                    this.styleItemUid = a.getObjuid();
                }
            });
        }else if (pageType.equals("nr")){
            type = "nr_djbt";
            contmStyleItems.forEach(a->{
                if(a.getItemType().equals("nr_djbt")&&a.getLvl() == lvl){
                    this.styleItemUid = a.getObjuid();
                }
            });
        }else if(pageType.equals("qz")){
            this.type = "qz_bt";
            contmStyleItems.forEach(a->{
                if(a.getItemType().equals("qz_bt")){
                    this.styleItemUid = a.getObjuid();
                }
            });

        }else if(pageType.equals("fj")){
            if(lvl == 1){
                type = "fj_bt";
                contmStyleItems.forEach(a->{
                    if(a.getItemType().equals("fj_bt")){
                        this.styleItemUid = a.getObjuid();
                    }
                });

            }else{
                type = "fj_djbt";
                contmStyleItems.forEach(a->{
                    if(a.getItemType().equals("qz_bt")&&a.getLvl() == lvl-1){
                        this.styleItemUid = a.getObjuid();
                    }
                });

            }
        }
    }

    public ContmContentDto (ContmContent contmContent, ContmContentDto parent){
        List<ContmContent> contmContentsTypeOfContent = contmContent.getContmContentsTypeOfContent();
        this.html = contmContentsTypeOfContent.stream().sorted(Comparator.comparingInt(ContmContent::getOrderCode)).map(ContmContent::getContent).reduce((a,b)->a+b).get();
        this.styleItemUid = contmContent.getStyleItemUid();
        this.id = contmContent.getObjuid();
        this.text = contmContent.getContent();
        this.type = contmContent.getItemType();
        this.pageType = contmContent.getPageType();
        this.orderCode = contmContent.getOrderCode().intValue();
        this.parent = parent;
        this.contMmainUid = contmContent.getContmMainUid();
        List<ContmContentDto> contmContentDtos = new ArrayList<>();

        contmContent.getContmContentsTypeOfTitle().forEach(a->{
            contmContentDtos.add(new ContmContentDto(a,this));
        });
        this.children = contmContentDtos;


    }
    @Autowired
    private ContmStyleItemDao contmStyleItemDao;
    private String styleItemUid;
    private String id;
    private String text;
    private String html;
    private String type;
    private String pageType;
    private Integer orderCode;
    private ContmContentDto parent;
    private String contMmainUid;
    private Integer lvl;
    private List<ContmContentDto> children;
    public JSONObject toJson(){
        JSONObject attr = new JSONObject();
        JSONArray childrenJsonArray = new JSONArray();
        attr.put("content",html);
        attr.put("type",type);
        attr.put("lvl",lvl);
        attr.put("contMmainUid",contMmainUid);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id",id);
        jsonObject.put("text",text);
        jsonObject.put("attributes",attr);
        for (int i = 0; i < children.size(); i++) {
            ContmContentDto contmDto = children.get(i);
            childrenJsonArray.add(contmDto.toJson());

        }
        return jsonObject;
    }
*/
private String id;
    private String ppId;
    private String name;
    private String title;
    private String content;
    private String parentTId;
    private String tId;
    private Integer level;
    private String pId;
    private String pageType;

    private String type;
    private List<ContmContentDto> children;

    public List<ContmContent> toContmContent(String contmBaseMainUid){

        List<ContmContent> contmContents = Lists.newArrayList();
        if (children!=null)
            for (int i = 0 ; i <children.size();i++){
                ContmContent contmContent = new ContmContent();
                excute(children.get(i),contmContent,contmBaseMainUid,this.getName(),i,new ContmContent());
                contmContents.add(contmContent);
            }
        return contmContents;
    }


    public void excute(ContmContentDto contmContentDto, ContmContent contmContent, String contmMainUid, String pageType, Integer i,ContmContent parent) {
        String objuid = StringUtil.generateRundom32String();
        contmContent.setObjuid(objuid);
        contmContent.setPageType(pageType);
        contmContent.setOrderCode(Byte.parseByte(Integer.toString(i)));
        contmContent.setItemType("bt");
        contmContent.setParentUid(parent.getObjuid());
        contmContent.setContent(contmContentDto.getTitle());
        contmContent.setAlias(contmContentDto.getName());
        contmContent.setContmMainUid(contmMainUid);

        if (contmContentDto.getContent()!=null&&!contmContentDto.getContent().equals("")){
            List<ContmContent> contmContents = Lists.newArrayList();
            contmContent.setContmContents(contmContents);
            contmContent.getContmContents().add(
                    ContmContent.builder().
                            objuid(StringUtil.generateRundom32String()).
                            pageType(pageType).
                            orderCode(Byte.parseByte(Integer.toString(i))).itemType("nr").
                            parentUid(objuid)
                            .content(contmContentDto.getContent()).
                            alias(contmContentDto.getName()).
                            contmMainUid(contmMainUid).build()
            );
        }
        if (contmContentDto.getChildren()!=null){

            for (int j = 0; j < contmContentDto.getChildren().size(); j++) {
                ContmContent contmContentTemp = new ContmContent();
                excute(contmContentDto.getChildren().get(j),contmContentTemp,contmMainUid,pageType,j ,contmContent);

                if (contmContent.getContmContents()==null){
                    List<ContmContent> objects = Lists.newArrayList();
                    contmContent.setContmContents(objects);
                }
                contmContent.getContmContents().add(contmContentTemp);
            }
        }
    }
}
